package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.ashGate;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CAshGateVideoBind;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TAshGateCollocation;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CAshGateVideoBindServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TAshGateCollocationServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.AshGateCollocationVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvNuo
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/ashGateCollocation")
public class AshGateCollocationController {
    @Autowired
    private TAshGateCollocationServiceImpl ashGateCollocationService;
    @Autowired
    private CAshGateVideoBindServiceImpl ashGateVideoBindService;

    @PostMapping("/getAshGateCollocation")
    public SzResult getAshGateCollocation(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String ashGateId = jsonObject.getString("ashGateId");
        QueryWrapper<TAshGateCollocation> ashGateCollocationQueryWrapper = new QueryWrapper<>();
        ashGateCollocationQueryWrapper.eq(StringUtils.isNotEmpty(ashGateId),"ASH_GATE_ID1",ashGateId);
        Page<TAshGateCollocation> page1 = ashGateCollocationService.page(new Page<TAshGateCollocation>(page, pageSize), ashGateCollocationQueryWrapper);
        List<AshGateCollocationVo> list = page1.getRecords().stream().map(item -> {
            AshGateCollocationVo ashGateCollocationVo = new AshGateCollocationVo();
            BeanUtils.copyProperties(item, ashGateCollocationVo);
            CAshGateVideoBind ashGate = ashGateVideoBindService.getById(item.getAshGateId1());
            if (ashGate != null) {
                ashGateCollocationVo.setAshGateName(ashGate.getAshgateName());
            }
            CAshGateVideoBind matchAshGate = ashGateVideoBindService.getById(item.getAshGateId2());
            if (matchAshGate != null) {
                ashGateCollocationVo.setMatchAshGateName(matchAshGate.getAshgateName());
            }
            return ashGateCollocationVo;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page1.getTotal());
        map.put("list", list);
        return SzResult.success(map);
    }
    @PostMapping("/addAshGateCollocation")
    public SzResult addAshGateCollocation(@RequestBody TAshGateCollocation ashGateCollocation) {
        QueryWrapper<TAshGateCollocation> tAshGateCollocationQueryWrapper = new QueryWrapper<>();
        tAshGateCollocationQueryWrapper.eq("ASH_GATE_ID1",ashGateCollocation.getAshGateId1());
        tAshGateCollocationQueryWrapper.eq("ASH_GATE_ID2",ashGateCollocation.getAshGateId2());
        TAshGateCollocation tAshGateCollocation = ashGateCollocationService.getOne(tAshGateCollocationQueryWrapper);
        if (tAshGateCollocation != null) {
            return SzResult.error("已有重复搭配");
        }
        QueryWrapper<TAshGateCollocation> ashGateCollocationQueryWrapper = new QueryWrapper<>();
        ashGateCollocationQueryWrapper.eq("ASH_GATE_ID1",ashGateCollocation.getAshGateId1());
        ashGateCollocationQueryWrapper.eq("ENABLE_FLAG",1);
        List<TAshGateCollocation> list = ashGateCollocationService.list(ashGateCollocationQueryWrapper);
        if (ashGateCollocation.getEnableFlag()==1) {
            if (list.size()==2){
                return SzResult.error("已启用三个装载点");
            }
        }

        return SzResult.success(ashGateCollocationService.save(ashGateCollocation));
    }
    @PostMapping("/updateAshGateCollocation")
    public SzResult updateAshGateCollocation(@RequestBody TAshGateCollocation ashGateCollocation) {
        //查询启用搭配
        QueryWrapper<TAshGateCollocation> ashGateCollocationQueryWrapper = new QueryWrapper<>();
        ashGateCollocationQueryWrapper.eq("ASH_GATE_ID1",ashGateCollocation.getAshGateId1());
        ashGateCollocationQueryWrapper.eq("ENABLE_FLAG",1);
        List<TAshGateCollocation> list = ashGateCollocationService.list(ashGateCollocationQueryWrapper);
        //查询重复搭配
        QueryWrapper<TAshGateCollocation> tAshGateCollocationQueryWrapper = new QueryWrapper<>();
        tAshGateCollocationQueryWrapper.eq("ASH_GATE_ID1",ashGateCollocation.getAshGateId1());
        tAshGateCollocationQueryWrapper.eq("ASH_GATE_ID2",ashGateCollocation.getAshGateId2());
        TAshGateCollocation tAshGateCollocation = ashGateCollocationService.getOne(tAshGateCollocationQueryWrapper);

        TAshGateCollocation gateCollocation = ashGateCollocationService.getById(ashGateCollocation.getId());

        if (gateCollocation.getAshGateId1().equals(ashGateCollocation.getAshGateId1()) && gateCollocation.getAshGateId2().equals(ashGateCollocation.getAshGateId2()) && gateCollocation.getEnableFlag() == ashGateCollocation.getEnableFlag()){
            return SzResult.success("true");
        }

        if (gateCollocation.getAshGateId1().equals(ashGateCollocation.getAshGateId1()) && gateCollocation.getAshGateId2().equals(ashGateCollocation.getAshGateId2())){
            if (ashGateCollocation.getEnableFlag()==1) {
                if (list.size()==2){
                    return SzResult.error("已启用三个装载点");
                }
            }
            return SzResult.success(ashGateCollocationService.updateById(ashGateCollocation));
        }else if (gateCollocation.getEnableFlag().equals(ashGateCollocation.getEnableFlag())){
            if (tAshGateCollocation != null) {
                return SzResult.error("已有重复搭配");
            }
        }else {
            if (ashGateCollocation.getEnableFlag()==1) {
                if (list.size()==2){
                    return SzResult.error("已启用三个装载点");
                }
            }
            if (tAshGateCollocation != null) {
                return SzResult.error("已有重复搭配");
            }
        }

        return SzResult.success(ashGateCollocationService.updateById(ashGateCollocation));
    }
    @PostMapping("/delAshGateCollocation")
    public SzResult delAshGateCollocation(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        return SzResult.success(ashGateCollocationService.removeById(id));
    }
}
